<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
<link rel="Start" href="index.html">
<link rel="next" href="Ast.html">
<link rel="Up" href="index.html">
<link title="Index of types" rel=Appendix href="index_types.html">
<link title="Index of exceptions" rel=Appendix href="index_exceptions.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Analysis" rel="Chapter" href="Analysis.html">
<link title="Ast" rel="Chapter" href="Ast.html">
<link title="Ast_info" rel="Chapter" href="Ast_info.html">
<link title="Environment" rel="Chapter" href="Environment.html">
<link title="Expression" rel="Chapter" href="Expression.html">
<link title="Filename_util" rel="Chapter" href="Filename_util.html">
<link title="Interpreter" rel="Chapter" href="Interpreter.html">
<link title="Jtemplate" rel="Chapter" href="Jtemplate.html">
<link title="Lexer" rel="Chapter" href="Lexer.html">
<link title="Library" rel="Chapter" href="Library.html">
<link title="Library_builtin" rel="Chapter" href="Library_builtin.html">
<link title="Library_io" rel="Chapter" href="Library_io.html">
<link title="Library_string" rel="Chapter" href="Library_string.html">
<link title="Parser" rel="Chapter" href="Parser.html">
<link title="Parser_util" rel="Chapter" href="Parser_util.html">
<link title="RuntimeError" rel="Chapter" href="RuntimeError.html"><title>Analysis</title>
</head>
<body>
<div class="navbar">&nbsp;<a href="index.html">Up</a>
&nbsp;<a href="Ast.html">Next</a>
</div>
<center><h1>Module <a href="type_Analysis.html">Analysis</a></h1></center>
<br>
<pre><span class="keyword">module</span> Analysis: <code class="code">sig</code> <a href="Analysis.html">..</a> <code class="code">end</code></pre>Create an optimized AST from the parsing phase AST
<p>

Pass 1:<ul>
<li>resolve all variable references, including closure variables</li>
<li>process imports and add declarations to AST</li>
<li>builds runtime AST</li>
<li>convert template definitions / instructions</li>
<li>evaluate operations on constants and replace with value in AST</li>
<li>determine if variables are initialized with a constant value (rather than an expression),
if a variable is written after being declared and if it is ever read after being declared</li>
<li>determine if a function is inlineable</li>
</ul>

Pass 2:
The second pass replaces all non function variables whose value have not been modified
with a constant value, and evaluates operations on constants , eliminates assignment
statements on constant values when the variable is not reassigned and not written,
inline functions<br>
<b>Author(s):</b> Tony BenBrahim < tony.benbrahim at gmail.com ><br>
<hr width="100%">
<pre><span class="keyword">val</span> <a name="VALcheck_errors"></a><a href="code_VALAnalysis.check_errors.html">check_errors</a> : <code class="type"><a href="Environment.html#TYPEanalysis_env">Environment.analysis_env</a> -> unit</code></pre><div class="info">
Prints all errors in an analysis environment and raises FatalExit if there are errors<br>
<b>Raises</b> <code>FatalExit</code> if there are errors in the environment<br>
<b>Returns</b> unit<br>
</div>
<div class="param_info"><code class="code">env</code> : analysis environment</div>
<pre><span class="keyword">val</span> <a name="VALcheck_warnings"></a><a href="code_VALAnalysis.check_warnings.html">check_warnings</a> : <code class="type"><a href="Environment.html#TYPEanalysis_env">Environment.analysis_env</a> -> <a href="Environment.html#TYPEanalysis_env">Environment.analysis_env</a></code></pre><div class="info">
Generates additional warnings about unused variables, then prints all warnings<br>
<b>Returns</b> analysis environment with newly added warnings<br>
</div>
<div class="param_info"><code class="code">env</code> : analysis environment</div>
<pre><span class="keyword">val</span> <a name="VALprint_name_info"></a><a href="code_VALAnalysis.print_name_info.html">print_name_info</a> : <code class="type"><a href="Environment.html#TYPEanalysis_env">Environment.analysis_env</a> -> unit</code></pre><div class="info">
Prints information about names found during analysis<br>
<b>Returns</b> unit<br>
</div>
<div class="param_info"><code class="code">env</code> : analysis environment</div>
<br>
FIRST PASS<ul>
<li>resolve all variable references, including closure variables</li>
<li>process imports and add declarations to AST</li>
<li>builds runtime AST</li>
<li>convert template definitions / instructions</li>
<li>evaluate operations on constants and replace with value in AST</li>
<li>determine if variables are initialized with a constant value (rather than an expression)</li>
<li>determine if a variable is written after being declared and if it is ever read after being declared</li>
<li>determine if a function is inlineable</li>
</ul>
<br>
<pre><span class="keyword">exception</span> <a name="EXCEPTIONTemplateError"></a>TemplateError <span class="keyword">of</span> <code class="type">string</code></pre>
<div class="info">
internal exception to signal an error in template processing.<br>
</div>
<pre><span class="keyword">val</span> <a name="VALcheck_template_nesting"></a><a href="code_VALAnalysis.check_template_nesting.html">check_template_nesting</a> : <code class="type"><a href="Ast.html#TYPEtemplate_spec">Ast.template_spec</a> list -><br>       (string, <a href="Environment.html#TYPElabel_pos">Environment.label_pos</a>) Hashtbl.t * (string * int) list</code></pre><div class="info">
Checks for invalid nesting in a template specification<br>
<b>Returns</b> an list of tuples containing the label and line offset where conflicts where found
*<br>
</div>
<div class="param_info"><code class="code">template_spec</code> : the template spec to check</div>
<pre><span class="keyword">val</span> <a name="VALgenerate_template_instr_function"></a><a href="code_VALAnalysis.generate_template_instr_function.html">generate_template_instr_function</a> : <code class="type">string * string list * <a href="Ast.html#TYPEreplacement_spec">Ast.replacement_spec</a> list * (string * int) -><br>       <a href="Environment.html#TYPEanalysis_env">Environment.analysis_env</a> -> <a href="Ast.html#TYPEruntime_statement">Ast.runtime_statement</a> * <a href="Environment.html#TYPEanalysis_env">Environment.analysis_env</a></code></pre><div class="info">
Generate a set of statements corresponding to a template instruction<br>
<b>Returns</b> a runtime statement for the instruction defining a function<br>
</div>
<div class="param_info"><code class="code">instruction</code> : instruction AST</div>
<div class="param_info"><code class="code">env</code> : runtime environment</div>
<pre><span class="keyword">val</span> <a name="VALfilter_imported_ast"></a><a href="code_VALAnalysis.filter_imported_ast.html">filter_imported_ast</a> : <code class="type"><a href="Ast.html#TYPEstatement">Ast.statement</a> list -> <a href="Ast.html#TYPEstatement">Ast.statement</a> list</code></pre><div class="info">
Filters an ast, returning only a list of declaration and import statement<br>
<b>Returns</b> a statement list containing only declarations and imports<br>
</div>
<div class="param_info"><code class="code">stmts</code> : the statement list to process</div>
<pre><span class="keyword">val</span> <a name="VALanalyze_variables"></a><a href="code_VALAnalysis.analyze_variables.html">analyze_variables</a> : <code class="type"><a href="Environment.html#TYPEanalysis_env">Environment.analysis_env</a> -><br>       <a href="Ast.html#TYPEstatement">Ast.statement</a> -> <a href="Ast.html#TYPEruntime_statement">Ast.runtime_statement</a> * <a href="Environment.html#TYPEanalysis_env">Environment.analysis_env</a></code></pre><div class="info">
find declarations and resolve references to variables. Since declarations are
visible in the entire scope in which they are defined, and not just after they are
declared, a breadth first search is necessary before recursively processing children
statements<br>
<b>Returns</b> an ast where all variables have been resolved to an absolute location, either
on a stack or in the global heap and an environment containing information about all
variables<br>
</div>
<div class="param_info"><code class="code">env</code> : an analysis environment</div>
<div class="param_info"><code class="code">ast</code> : the intermediate ast</div>
<br>
SECOND PASS<ul>
<li>replace all constant declarations with Noop</li>
<li>replace all constant variables with their value</li>
<li>replace all constant expressions with the computed value</li>
<li>replace all calls to inlineable functions with an expression</li>
</ul>
<br>
<pre><span class="keyword">val</span> <a name="VALinline_expr_replace"></a><a href="code_VALAnalysis.inline_expr_replace.html">inline_expr_replace</a> : <code class="type">int -><br>       int -><br>       <a href="Ast.html#TYPEruntime_expression">Ast.runtime_expression</a> list -><br>       <a href="Ast.html#TYPEruntime_expression">Ast.runtime_expression</a> -> <a href="Ast.html#TYPEruntime_expression">Ast.runtime_expression</a></code></pre><div class="info">
replaces an expression from an inlined function with the corresponding
values from a function call expression list<br>
<b>Returns</b> the inline expression with the arguments replacing the former local args<br>
</div>
<div class="param_info"><code class="code">depth</code> : the stack depth, for sanity checking</div>
<div class="param_info"><code class="code">numargs</code> : the number of arguments</div>
<div class="param_info"><code class="code">expr</code> : the inline expression</div>
<pre><span class="keyword">val</span> <a name="VALreplace_constant"></a><a href="code_VALAnalysis.replace_constant.html">replace_constant</a> : <code class="type"><a href="Environment.html#TYPEanalysis_env">Environment.analysis_env</a> -><br>       int list -> <a href="Ast.html#TYPEruntime_expression">Ast.runtime_expression</a> -> <a href="Ast.html#TYPEruntime_expression">Ast.runtime_expression</a></code></pre><div class="info">
Replace non modified variables with their declared value<br>
<b>Returns</b> an expression with constant variables replaced by their value<br>
</div>
<div class="param_info"><code class="code">env</code> : analysis environment</div>
<div class="param_info"><code class="code">inline_uids</code> : list of inlined functions to avoid recursively inlining recursive inlinable functions</div>
<pre><span class="keyword">val</span> <a name="VALpass2"></a><a href="code_VALAnalysis.pass2.html">pass2</a> : <code class="type"><a href="Environment.html#TYPEanalysis_env">Environment.analysis_env</a> -> <a href="Ast.html#TYPEruntime_statement">Ast.runtime_statement</a> -> <a href="Ast.html#TYPEruntime_statement">Ast.runtime_statement</a></code></pre><div class="info">
Looks for expressions where constants can be substituted<br>
</div>
<div class="param_info"><code class="code">env</code> : analysis environment</div>
<pre><span class="keyword">val</span> <a name="VALanalyze"></a><a href="code_VALAnalysis.analyze.html">analyze</a> : <code class="type"><a href="Ast.html#TYPEstatement">Ast.statement</a> -> <a href="Ast.html#TYPEruntime_statement">Ast.runtime_statement</a> * <a href="Environment.html#TYPEanalysis_env">Environment.analysis_env</a></code></pre><div class="info">
Analyzes an AST, generates a runtime AST<br>
<b>Returns</b> a tuple of the runtime AST and analysis environment<br>
</div>
<div class="param_info"><code class="code">ast</code> : a parsing AST</div>
</body></html>